package org.example.service.ack;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author Tang
 * @classname InfoService
 * @description [  ]
 * @date 2023/4/10 22:03
 */
@RabbitListener(bindings = {
        @QueueBinding(
                value = @Queue(name = "test-ack-queue"),
                exchange = @Exchange(value = "test-ack-exchange")
                ,key = "test-ack-key"
        )
})
//@Component
public class AckService {

    @RabbitHandler
    public void infoReceive(String msg, @Headers Map<String, Object> headers, Channel channel) {
        Long deliveryTag  = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("info-Receive: " + msg);
    }

}